/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef UNARYOPNODE_H
#define UNARYOPNODE_H

#include <ExpressionTreeNode.h>
#include <FunTable.h>


class UnaryOpNode : public ExpressionTreeNode
{
public:
    UnaryOpNode(const QString& info);

    /**
     *\fn ~UnaryOpNode()
     *\brief Constructor
     */
    inline virtual ~UnaryOpNode() {
    }

    virtual ExpressionTreeNode* copy() const;


    virtual Real eval() const;

    virtual SmartPtr<ExpressionTreeNode> derive(const QString& varName) const;

    /**
     *\fn QString info() const
     *\brief Returns the operator symbol as a string.
     * Reimplemented from ExpressionTreeNode .
     */
    inline virtual QString info() const {
        return _info;
    }

    /**
     *\fn SmartPtr<ExpressionTreeNode> right()
     *\brief Returns a pointer to the right subtree root.
     * Reimplemented from ExpressionTreeNode .
     *
     */
    inline virtual SmartPtr<ExpressionTreeNode> right() {
        return _child;
    }

    /**
     *\fn const SmartPtr<ExpressionTreeNode> right() const
     *\brief Returns a constant pointer to the right subtree root.
     * Reimplemented from ExpressionTreeNode .
     */
    inline virtual const SmartPtr<ExpressionTreeNode> right() const {
        return _child;
    }

    /**
     *\fn void setRight(SmartPtr<ExpressionTreeNode> node)
     *\brief Assigns the right subtree to the tree having as root the argument passed to the method.
     *\param node A pointer to the root of the new right subtree.
     * If the right subtree is not null it will be  previously destroyed and memory freed.
     * Reimplemented from ExpressionTreeNode .
     */
    inline virtual void setRight(SmartPtr<ExpressionTreeNode> node)  throw (OperandsException){
        _child = node;
    }

    /**
     *\fn bool isUnaryOp() const
     *\brief Returns true.
     */
    inline virtual bool isUnaryOp() const {
        return true;
    }
    /**
     *\fn bool isArithmetic() const
     *\brief Returns true.
     */
    inline virtual bool isArithmetic() const {
        return true;
    }
    virtual QString toPrefixString() const;

    virtual QString toPostfixString() const;

    virtual QString toInfixString() const;

    virtual int precedence() const;

    virtual SmartPtr<ExpressionTreeNode> firstLevelSimplification() const;

    virtual void bind(int varPos,  const Real * var) const;

    virtual void bind(const QString& varName,  const Real * var) const;

    virtual ExpressionTreeNode* replace(const QString& varName, ExpressionTreeNode* tree) const;

    virtual ExpressionTreeNode* replace(int varPos, ExpressionTreeNode* tree) const;

    virtual void resetMarker() const{
    }

protected:
    SmartPtr<ExpressionTreeNode> _child;
    MathUnaryFun _op;
    ExpressionTreeOp _derivativeBuilder;
    QString _info;
};

#endif // UNARYOPNODE_H
